package com.mangowang.module.main.ui;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.protobuf.InvalidProtocolBufferException;
import com.mangowang.lib.common.base.BaseActivity;
import com.mangowang.lib.common.net.Apis;
import com.mangowang.lib.common.net.NetworkManager;
import com.mangowang.lib.common.net.interfaces.OnRequestCallBack;
import com.mangowang.lib.common.proto.MemberOuterClass;
import com.mangowang.lib.common.util.GsonUtils;
import com.mangowang.lib.common.util.StatusBarUtil;
import com.mangowang.module.main.R;
import com.mangowang.module.main.R2;
import com.mangowang.module.main.adapter.MessageAdapter;
import com.mangowang.module.main.adapter.SnCodeAdapter;
import com.mangowang.module.main.bean.AllowSnListBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class AllowSnListActivity extends BaseActivity {

    @BindView(R2.id.back_iv)
    ImageView backIv;
    @BindView(R2.id.tv_title)
    TextView tvTitle;
    @BindView(R2.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R2.id.tv_sn)
    TextView tv_sn;
    @BindView(R2.id.tv_submit)
    TextView tv_submit;

    private int index = -1;  // 选中默认值

    private SnCodeAdapter mAdapter;
    public AllowSnListBean snData;
    private List<Integer> integers = new ArrayList<>();
    private LinearLayoutManager linearLayoutManager;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);
        ButterKnife.bind(this);
        StatusBarUtil.setStatusBarLightMode(this, Color.parseColor("#FFFFFF"), true);
        String snDataString =  getIntent().getStringExtra("snData");
         snData =  GsonUtils.deSerializedFromJson(snDataString, AllowSnListBean.class);


        initRecyclerView();
        initListener();
        tvTitle.setText("SN列表");
        tv_sn.setText(snData.getShowSnNumber()  + "(" + snData.getTotalSnNumber() + ")");
        tv_submit.setOnClickListener(v -> {
            Intent intent = new Intent();
            intent.putExtra("snData",GsonUtils.serializedToJson(snData));
            setResult(800,intent);
            finish();
        });

        loadData();
    }

    private void initListener() {
        backIv.setOnClickListener(view -> finish());

    }

    private void loadData() {

        mAdapter.addAll(snData.getSnList());
    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    private void initRecyclerView() {
        linearLayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(mAdapter = new SnCodeAdapter(this));
        if (getIntent().getStringExtra("type").equals("only")) {
            mAdapter.setOnItemClickListener(position -> {
                AllowSnListBean.SnMessage snBean = snData.getSnList().get(position);
                if (snBean.getIs_transfer().equals("1")) return;
                snBean.setSelect(!snBean.isSelect());
                if (snBean.isSelect()) {
                    integers.add(position);
                } else {
                    integers.remove(Integer.valueOf(position));
                }
                mAdapter.notifyDataSetChanged();
                snData.getSelectedSnList().clear();
                for (int i = 0; i < snData.getSnList().size(); i++) {
                    if (snData.getSnList().get(i).isSelect() && snData.getSnList().get(i).getIs_transfer().equals("0")) {
                        snData.getSelectedSnList().add(snData.getSnList().get(i));
                    }
                }
                snData.setSelectSnNumber(String.valueOf(snData.getSelectedSnList().size()));
                tv_submit.setText("确认（已选择"+snData.getSelectedSnList().size()+"台）");
            });
        } else {
            mAdapter.setOnItemClickListener(position -> {
                AllowSnListBean.SnMessage snBean = snData.getSnList().get(position);
                if (snBean.getIs_transfer().equals("1")) return;
                if (index == -1) {  //index == -1
                    index = position;
                    for (AllowSnListBean.SnMessage bean : snData.getSnList()) {
                        if (bean.isSelect()) {
                            bean.setSelect(false);
                        }
                    }
                    snBean.setSelect(true);
                } else {
                    integers.clear();
                    for (int i = 0; i < snData.getSnList().size(); i++) {
                        if (snData.getSnList().get(i).isSelect()) {
                            snData.getSnList().get(i).setSelect(false);
                        }
                    }
                    if (position >= index) {
                        for (int i = index; i <= position; i++) {
                            snData.getSnList().get(i).setSelect(true);
                            integers.add(i);
                        }
                    } else {
                        for (int i = index; i >= position; i--) {
                            snData.getSnList().get(i).setSelect(true);//!snBean.isSelect()
                            integers.add(i);
                        }
                    }
                }
                mAdapter.notifyDataSetChanged();
                snData.getSelectedSnList().clear();
                for (int i = 0; i < snData.getSnList().size(); i++) {
                    if (snData.getSnList().get(i).isSelect() && snData.getSnList().get(i).getIs_transfer().equals("0")) {
                        snData.getSelectedSnList().add(snData.getSnList().get(i));
                    }
                }
                snData.setSelectSnNumber(String.valueOf(snData.getSelectedSnList().size()));
                tv_submit.setText("确认（已选择"+snData.getSelectedSnList().size()+"台）");
            });
        }

    }


}
